#include <gflags/gflags.h>
#include <stdio.h>
#include <lcm/lcm-cpp.hpp>
#include<iostream>
#include<unistd.h>
#include "../include/serial/serial.h"
#include <list>
#include <mutex>
#include "../include/parsedata.h"
using namespace std;
// Create serial port
list<string> hugebuffer;
mutex m;
//  线程体，不断地读取数据
	void *parsedataFunction(void *arg)
	{
		std::cout<<"ReadFunction"<<std::endl;
		string port("/dev/ttyUSB0");
		unsigned long baud = 230400;
		serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
		int count = 100;			
		ParseData  pd;
		//my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
		string buffer;
		while(1)
		{
			buffer= my_serial.read(8);
			m.lock();
			hugebuffer.push_back(buffer);
			m.unlock(); 
			buffer.clear();
		}
		cout<<"parsedata is done"<<endl;
	}
	
	void *writeFileFunction(void *arg)
	{
		std::cout<<"writeFileFunction"<<std::endl;
		int i, length = 0;
		string str1;
		ofstream ofs;
		time_t now = time(0);
		tm *ltm = localtime(&now);
		string rtime = 
						to_string(1900+ltm->tm_year)+"_"+
						to_string(1+ltm->tm_mon)+"_"+
						to_string(ltm->tm_mday)+"_"+
						to_string(ltm->tm_hour)+"_"+
						to_string(ltm->tm_min/10);
		str1 = "imu_data_"+rtime+".txt";
		ofs.open(str1,ios::app);
		long long point= 0;
		long fronttime=0;
		string parsedata;
		ParseData  pd;
		std::vector<u_int8_t>  strRecv;
		sleep(2);
		while(1)
		{	
			usleep(20);
			if(hugebuffer.size()<1){
				continue;
			}else{
			m.lock();
			parsedata = hugebuffer.front(); 
			//cout<<parsedata<<endl;
			hugebuffer.pop_front();
			m.unlock();}
			
			for(i = 0; i < parsedata.size(); i++)
			{				
				strRecv.push_back(parsedata[i]);
			}
			pd.ljlparse_data(strRecv,ofs);
		}
		ofs.close();	
	}
void *SerialTestFun(void *arg)
	{
		std::cout<<"SerialTestFun"<<std::endl;

		string port("/dev/ttyUSB0");
		unsigned long baud = 230400;
		serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
		ParseData  pd;
		my_serial.setTimeout(serial::Timeout::max(), 20, 0, 20, 0);
		string buffer;
		string str1;
		ofstream ofs;
		time_t now = time(0);
		tm *ltm = localtime(&now);
		string rtime = 
						to_string(1900+ltm->tm_year)+"_"+
						to_string(1+ltm->tm_mon)+"_"+
						to_string(ltm->tm_mday)+"_"+
						to_string(ltm->tm_hour)+"_"+
						to_string(ltm->tm_min/10);
		str1 = "imu_data_"+rtime+".txt";
		ofs.open(str1,ios::app);
		
		int count = 1024;
		while(count>0)
		{	
			my_serial.read(buffer,8);
			m.lock();
			ofs<<buffer;
			m.unlock(); 
			count--;
		}
		ofs.close();	
	}
int main()
{
	pthread_t parsedataThread;
	pthread_t writeToFileThread;
	pthread_t serialThread;

	std::cout<<"you create a parsedataThread!!"<<std::endl;
	if ( pthread_create( &parsedataThread, NULL, parsedataFunction, NULL) ) 
	{
		printf("Error creating readThread.\n");
	}

	std::cout<<"you create a writeToFileThread!!"<<std::endl;
	if ( pthread_create( &writeToFileThread, NULL, writeFileFunction, NULL) ) 
	{
		printf("Error creating readThread.\n");
	}

	// std::cout<<"you create a writeToFileThread!!"<<std::endl;
	// if ( pthread_create( &serialThread, NULL, SerialTestFun, NULL) ) 
	// {
	// 	printf("Error creating readThread.\n");
	// }

	while(1){
		sleep(60);
		break;
	}
	//pthread_join(writeToFileThread,NULL);
	return 0;
}
